package com.handong.handserialprogram.module.cofigring;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.handong.handserialprogram.R;
import com.handong.handserialprogram.model.entities.FootRingNewEntity;
import com.handong.handserialprogram.module.base.BaseActivity;
import com.handong.handserialprogram.ui.radar.RadarScanView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by userKey on 2018/6/21. 配置脚环参数下发
 */

public class ConfigRingListAcivity extends BaseActivity<ConfigRingListPresenter> implements ConfigRingListView,ConfigRingAdaper.CallbackListener {

    private RecyclerView mSetRingRV;
    private RadarScanView mRadarScan;
    private List<FootRingNewEntity> mRingEntities=new ArrayList<>();
    private ConfigRingAdaper mAdaper;

    @Override
    protected int getContentViewID() {
        return R.layout.activity_configringlist;
    }

    @Override
    protected ConfigRingListPresenter createPresenter() {
        return new ConfigRingListPresenter(this);
    }

    @Override
    protected void initViewByID() {
        initHeaderLayout(false);
        setCenterTitle("脚环配置");
        mSetRingRV = (RecyclerView) findViewById(R.id.footRingRV);
        mRadarScan = (RadarScanView) findViewById(R.id.scanView);
    }

    @Override
    protected void initViewDataRes() {
        mPresenter.scanFootRings();
        mAdaper = new ConfigRingAdaper(this,mRingEntities,this);
        mSetRingRV.setLayoutManager(new LinearLayoutManager(this));
        mSetRingRV.setAdapter(mAdaper);
    }
    @Override
    public void onItemClick(FootRingNewEntity entity) {

    }
    @Override
    public void onConfigClick(FootRingNewEntity entity) {
            mPresenter.startConfig(entity.getRingID());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.headerBackLayout:
                finish();
                break;
            case R.id.headerOptionLayout:
                break;
            default:
                super.onClick(v);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mRadarScan != null) {
            mRadarScan.stopAnimation();
            mRadarScan.onDestroy();
            mRadarScan = null;
        }
    }

    @Override
    public void showFootRingList(List<FootRingNewEntity> list) {
        mRingEntities.clear();
        for (FootRingNewEntity entity:list) {
            if(!entity.isBingding()&&!mRingEntities.contains(entity)){
                mRingEntities.add(entity);
            }
        }
        if (!mRingEntities.isEmpty()) {
            if(mRadarScan.isShown()){
                mRadarScan.stopAnimation();
                mRadarScan.setVisibility(View.GONE);
                mSetRingRV.setVisibility(View.VISIBLE);
            }
        }else{
            if(!mRadarScan.isShown()){
                mRadarScan.startAnimation();
                mRadarScan.setVisibility(View.VISIBLE);
                mSetRingRV.setVisibility(View.GONE);
            }
        }
        mAdaper.notifyDataSetChanged();
    }


}
